<%@ page contentType="text/html; charset=UTF-8" language="java" %>

<% 
	String context = request.getContextPath();
	String formulaPath = context + "/component/formula";
%>

<html>

<head>	
	<title>公式设置</title>
	
	<script>
		function getContext(){return "<%=context%>";};
		function getRelationContext(){return formulaPath + "/js";}	
	</script>

	<SCRIPT LANGUAGE="JavaScript" src="<%=formulaPath%>/js/Map.js"></SCRIPT>
	<SCRIPT LANGUAGE="JavaScript" src="<%=formulaPath%>/js/util/ValueUtil.js"></SCRIPT>
	<SCRIPT LANGUAGE="JavaScript" src="<%=formulaPath%>/js/util/FormulaUtil.js"></SCRIPT>
	
	<script type="text/javascript">

		String.prototype.trim = function(){
			return this.replace(/^\s*|\s*$/g,"");
		}

		var valueUtil = new ValueUtil();
		var formulaUtil = new FormulaUtil();

		var valueMap = valueUtil.getValueMap();
		var varMap = valueUtil.getVarMap();
		
		var map = new Map();//用于验证公式
		map.put("职称","高级");
		map.put("学历","博士");
		map.put("工作状态","在职");
		map.put("级别","正科");
		map.put("职务","班子正职");
		map.put("私车公用","是");
	    map.put("性别","男");
	    map.put("人员类别","在职公务员");
		map.put("出生日期","1958-10-25");
		map.put("工作时间","2005-10-25");
		map.put("离退休时间","2007-10-25");

		map.put("基本工资","0");
		map.put("职务工资","0");
		map.put("职务补贴","0");
		map.put("住房补贴","0");
		map.put("奖励工资","0");
		map.put("水电费","0");
		
		function isInArray(str,arr){
	    	for(var i=0;i<arr.length;i++){
	    		//alert(str==arr[i]);
	    		if(str==arr[i]){
	    			return true;
	    		}
	    	}
	    	//if(str!=16) alert(str + ",false");
	    	return false;
	    }
		
		var notInSideChars = ['+','-','*','/','=','.','<','>'];//不能在最前和最后的字符
		var operChars = ['+','-','*','/','='];//操作符号
	
		//验证公式:右边的右括号数要小于等于左边的左括号数
		function doValidate(isAlert){
			var formulaStr = document.getElementById("formula").value.trim();
			if(formulaStr=="") return true;
			//alert("formulaStr = " + formulaStr);
			
			var error = "公式的语法不对！";
			
			var leftBrackets = 0;//左括号数
			var rightBrackets = 0;//右括号数
			for(var i=0;i<formulaStr.length;i++){
				var temp = formulaStr.charAt(i);
				if(temp=='('){leftBrackets++;}
				if(temp==')'){rightBrackets++;}
				if(rightBrackets>leftBrackets){
					alert("右括号数大于左括号数！");return false;
				}
			}
			if(leftBrackets!=rightBrackets){
				alert("左右括号数不相等！");return false;
			}
			
			//验证[],{}的配对
			for(var i=0;i<formulaStr.length;i++){
				var c = formulaStr.charAt(i);
				if(c=='[' || c=='{'){
					var varExp = "";
					for(var j=i+1;j<formulaStr.length;j++){
						var c2 = formulaStr.charAt(j);
						if(c2=='[' || c2=='{'){
							alert("引用的项目不能嵌套！");//[],{}不能嵌套
							return false;
						}else if(c2==']' || c2=='}'){
							if(varExp==""){
								alert("引用的变量不能为空！");//变量不能为空
								return false;
							}
							var value = varMap.get(varExp);
							//alert("value = " + value);
							if(value!=1){
								alert("引用的变量"+c+varExp+c2+"不存在！");//不是变量
								return false;
							}
							i = j;
							break;
						}else{
							varExp += c2;
						}
					}
				}
			}
			
			if(isAlert) alert("公式正确！");
			return true;
		}
		
		
		
		
		var textareaId = "formula";
		var start=0;
	    var end=0;
	    //将点击的内容放到公式列表里
	    //需要验证输入符号的合法性：如，"0"不能为第一个数字，"."前需要有数字,"运算符号"不能作为第一个字符 等等
	    //项目名称必须存在(还没有验证)
		function doClick(obj,flag){
			//alert("doClick:" + obj.value);
			var insertValue = obj.value;
			if(insertValue.indexOf("如果")!=-1){
				insertValue += ";";
			}
			if(flag == 'fun') insertValue += "()";
			else if(flag == 'item') insertValue = "{" + insertValue + "}";
			else if(flag == 'conValue') insertValue = "{" + insertValue + "}";
			else if(flag == 'con'){
				//insertValue = "@" + insertValue;
				insertValue = "[" + insertValue + "]";
			}
	        var textBox = document.getElementById(textareaId);
	        var pre = textBox.value.substr(0, start);
	        var post = textBox.value.substr(end);
	        textBox.value = pre + insertValue + post;
	        
	        //记住赋值后的位置
	        start += insertValue.length;
	        end += insertValue.length;
	    }
	    
	    var useKeys = [0,13,32,8,46,37,38,39,40,48,49,50,51,52,53,54,56,57,96,97,98,99,100,101,102,103,104,105,191,190,187,189,106,107,109,110,111];//可以按下的键:空格，换行，退格，删除,上下左右键,数字键, / , . , = , -(包括小键盘) 
	    var useShiftKeys = [187,56,57,48,188,190];// + , * , ( , ) , < , >
	    
	    function savePos(textBox){
	    	var code = event.keyCode;
	    	//alert("code = " + code);
	    	//除了能按下的键外，其它不能输入
	    	if(!event.shiftKey && !isInArray(code,useKeys)){
	    		event.returnValue=false;
	    	}
	    	if(event.shiftKey){//有shift的组合键
	    		if(!isInArray(code,useShiftKeys)){
	    			event.returnValue=false;
	    		}
	    	}
	    	var pos = formulaUtil.getPosition(textBox,start,end);
	    	start = pos[0];
	    	end = pos[1];
		}
	</script>
</head>

<body>

<table>
  
  <!-- 按钮内容 -->
  <tr>
    <td align="right">
    	<input type="button" value="检验公式" class="button-fou" onclick="doValidate(true)" />
    	<input type="button" value="清除公式" class="button-fou" onclick="document.getElementById('formula').value='';" />
    	<input type="button" value="确定()" class="button-fou" onclick="doOK()"  />
    	<input type="button" value="取消()" class="button-fou" onclick="doCancel()" />
    </td>
  </tr>
  
  <!-- 公式内容 -->
  <tr>
    <td>
    	<textarea rows="7" cols="100" id="formula" name="formula" onfocus="savePos(this)"
    			  onKeydown="savePos(this)" onKeyup="savePos(this)" 
                  onmousedown="savePos(this)" onmouseup="savePos(this)"></textarea>
    </td>
  </tr>
  
  <!-- 项目，项目值，运算符，条件  -->
  <tr>
    <td>
    	<table>
    		<tr>
    			<!-- 项目 -->
    			<td>
    				<font size="2">条件：</font><br>
    				<select name="item" size="12" style="width: 120" multiple="multiple" onchange="valueUtil.changeItemValue(this)" ondblclick="doClick(this,'con')">
    					<option value="职称">职称</option>
    					<option value="学历">学历</option>
    					<option value="工作状态">工作状态</option>
    					<option value="级别">级别</option>
    					<option value="职务">职务</option>
    					<option value="私车公用">私车公用</option>
    					<option value="性别">性别</option>
    					<option value="人员类别">人员类别</option>
    					<option value="出生日期">出生日期</option>
    					<option value="工作时间">工作时间</option>
    					<option value="离退休时间">离退休时间</option>
    				</select>
    			</td>
    			
    			<td width="1"></td>
    			
    			<!-- 项目值 -->
    			<td>
    			<font size="2">条件值：</font><br>
    			<select id="itemValue" name="itemValue" size="12" style="width: 120" multiple="multiple" ondblclick="doClick(this,'conValue')" >		
   					<option value="高级">高级</option>
   					<option value="中级">中级</option>
   					<option value="初级">初级</option>
   					<option value="未评职称">未评职称</option>
   				</select>
    			</td>
    			
    			<td width="1"></td>
    			<td>
    				<font size="2">工资项目：</font><br>
    				<select name="item" size="12" style="width: 120" multiple="multiple" ondblclick="doClick(this,'item')">
						<option value="基本工资">基本工资</option>
						<option value="职务工资">职务工资</option>
						<option value="职务补贴">职务补贴</option>
						<option value="住房补贴">住房补贴</option>
						<option value="奖励工资">奖励工资</option>
						<option value="水电费">水电费</option>
    				</select>
    			</td>

    			

    			<td width="1"></td>
    			
    			<!-- 运算符 -->
    			<td valign="top">
    				<fieldset class="fieldset"  align="center">
                	<legend>运算符</legend>
    				<table>
    					<tr>
    						<td>
    							<table>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="1" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="4" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="7" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="0" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="(" onClick="doClick(this)" /></td></tr>
    							</table>
    						</td>
    						
    						<td>
    							<table>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="2" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="5" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="8" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="." onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value=")" onClick="doClick(this)" /></td></tr>
    							</table>
    						</td>
    						
    						<td>
    							<table>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="3" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="6" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="9" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="=" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="<" onClick="doClick(this)" /></td></tr>
    							</table>
    						</td>
    						
    						<td>
    							<table>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="+" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="-" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="*" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value="/" onClick="doClick(this)" /></td></tr>
    								<tr><td><input type="button" style="width:25;height:27" class="button-fou" value=">" onClick="doClick(this)" /></td></tr>
    							</table>
    						</td>
    					</tr>
    				</table>
    				</fieldset>
    			</td>
    			
    			<td width="1"></td>
    			
    			<td valign="top">
    			<table>
    				<tr><td>
    				<fieldset class="fieldset"  align="center">
                	<legend>逻辑</legend>
    				<table>
    					<tr>
    						<td>
    							<input type="button" style="width:100;height:25" class="button-fou" value=" 如果  则  " onClick="doClick(this)" />
    						</td>
    					</tr>
    					<tr>
    						<td>
    							<input type="button" style="width:100;height:25;" class="button-fou" value="否则    " onClick="doClick(this)" />
    						</td>
    					</tr>
    					<tr>
    						<td>
    							<input type="button" style="width:45;height:25" class="button-fou" value="且" onClick="doClick(this)" />
    							<input type="button" style="width:45;height:25" class="button-fou" value="或" onClick="doClick(this)" />
    						</td>
    					</tr>
    				</table>
    				</fieldset>
    				</td></tr>
    				
    				<tr>
		    			<td valign="top">
		    				<fieldset class="fieldset"  align="center">
		                	<legend>函数</legend>
		                	<table>
		    					<tr>
		    						<td>
		    							<input type="button" style="width:50;height:22" class="button-fou" value="year" onClick="doClick(this,'fun')" />
		    						</td>
		    					</tr>
		    				</table>
		    				</fieldset>
		    			</td>
    				</tr>
    				
    			</table>
    			</td>
    			
    		</tr>
    	</table>
    </td>
  </tr>
  
  <tr>
  	<td>
  	<hr>
  	<font size="2">说明：<br>
  	可用的键： 空格键，回车键，退格键，删除键，上下左右键，数字键 <br>
  	比较符号： 不等于：<> ，大于等于：>= ，小于等于：<= <br>
  	标志符号： []表示变量，{}表示条件值 <br>
  	时间函数： 当year不带参数时表示当前年 <br>
  	时间格式： YYYY-MM-DD <br>
  	结束标志： "如果"结束的标志用';'表示
  	</font>
  	<hr>
  	</td>
  </tr>

</table>

</body>

</html>

<script type="text/javascript">
<!--
	var formulaValue=window.dialogArguments;
	var formulaTextAreaObj = document.getElementById("formula");
	//formulaTextAreaObj.value = formulaValue;
	formulaTextAreaObj.value = "";
	
	//保存公式
	function doOK(){
		if(doValidate()){
			var formulaName = document.getElementById("formula").value.trim();
			if(formulaName!=""){
				//将名称转换成id
				var formula = "";
				var name = "";
				for(var i=0;i<formulaName.length;i++){
					var c = formulaName.charAt(i);
					if(c=='{'){
						name = "";
						for(var j=i+1;j<formulaName.length;j++){//寻找对应的括号
							var c2 = formulaName.charAt(j);
							if(c2=='}'){
								//alert("conName = " + name);
								var valueId = valueMap.get(name);
								if(valueId==null || valueId==''){
									valueId = name;
								}
								formula += '{' + valueId + '}';
								//alert("formula = " + formula);
								i = j;
								break;
							}else{
								name += c2;
							}
						}
					}
					else{
						formula += c;
					}
				}
				//alert("formula = " + formula);
			}
			window.returnValue= [formula,formulaName];
			window.close();			
		}
	}
	
	function doCancel(){
		window.close();
	}
	
//-->
</script>
